package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import application.GameManager;
import application.XmlEncodeToFile;

public class HighScoreFrame extends JFrame implements ActionListener{
	
	private final String[] _columnNames = {"Rank", "Name", "Score"};
	private Object[][] _data;
	private GameManager _manager;

	public HighScoreFrame(GameManager manager,String name) {
		super("High Score");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this._manager = manager;
		//add new high score
		this._manager.get_highScoreTable().addScore(name,this._manager.getScore());
		XmlEncodeToFile.writeToFile(this._manager.get_highScoreTable());
		this._data = manager.get_highScoreTable().toObjectArrays();
		JTable table = new JTable(this._data,this._columnNames);
		JScrollPane scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(scrollPane,BorderLayout.CENTER);
		JButton okButton = new JButton("OK");
		okButton.addActionListener(this);
		this.getContentPane().add(okButton,BorderLayout.SOUTH);
		this.setSize(400, 250);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		this._manager.get_StartGame().setVisible(true);
		this.setVisible(false);		
	}

}
